package com.sgb.member.view_model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberLevelApproveBean;
import com.sgb.member.service.MemberAPI;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class RatingBusinessModel extends AndroidViewModel {
    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<Boolean> empty = new MutableLiveData<>();
    public MutableLiveData<Boolean> hasLoadMore = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> memberLeveLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> memberStateLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> memberZhuTiLiveData = new MutableLiveData<>();
    public MutableLiveData<List<MemberLevelApproveBean>> mLeverData = new MutableLiveData<>();
    private final MemberAPI api;

    private int current;

    public RatingBusinessModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    public void getMixMemberLevelApproveListByPage(boolean isLoadMore, String level,String status,String memberType) {
        dialog.setValue(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", 20);
        map.put("pageNum", currentPage);
        map.put("level", level);
        map.put("status", status);
        map.put("memberType", memberType);
        api.getMixMemberLevelApproveListByPage(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberLevelApproveBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberLevelApproveBean>>> data) {
                dialog.setValue(false);
                empty.setValue(false);
                current = currentPage;
                int total = data.getData().getPages();
                hasLoadMore.setValue(current * 20 < total);
                mLeverData.setValue(data.getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberLevelApproveBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                dialog.setValue(false);
                empty.setValue(true);
            }
        });
    }

    public void getH5Url(MemberLevelApproveBean approveBean, SimpleResult<String> simpleResult) {
        dialog.setValue(true);
        api.getUrlSetting("memberEvaluate").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                dialog.setValue(false);
                String url = data.getData().getUrl() + "?no=" + approveBean.getNo() + "&showOperation=&auditId=" + approveBean.getProcessId();
                MLog.d("api", "H5跳转链接  " + url);
                simpleResult.onResult(url);
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                super.onFailure(call, t);
                dialog.setValue(false);
            }
        });
    }

    public void getMemberLevelData() {
        api.getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    memberLeveLiveData.postValue(data.getData());
                }
            }
        });
    }

    public void getMemberStateData() {
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("1", "待调级"));
        list.add(new CommonPopEntity("2", "调级成功"));
        list.add(new CommonPopEntity("3", "调级失败"));
        memberStateLiveData.postValue(list);
    }

    public void getMemberZhuTi() {//1审批中2通过3拒绝  不限，待调级，调级成功，调级失败
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(new CommonPopEntity("", "全部"));
        list.add(new CommonPopEntity("1", "企业会员"));
        list.add(new CommonPopEntity("2", "个人会员"));
        memberZhuTiLiveData.postValue(list);
    }

}
